package com.example.luobaomessage;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;


import okhttp3.Call;
import okhttp3.Callback;

import okhttp3.OkHttpClient;
import okhttp3.Request;

import okhttp3.Response;


public class Fragment1 extends Fragment {


    private TextView ftext1;
    ImageView imageView;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        ftext1 = view.findViewById(R.id.ftext1);
        imageView = view.findViewById(R.id.imageView);


        getCarmeesage(MyApp.token);



    }



    private void getCarmeesage(String token) {
        String url = "http://124.93.196.45:10001//dev-api/bs-car/car/list";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).addHeader("Authorization",token).get().build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();

                getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {

                            JSONObject object = new JSONObject(s);
                            JSONArray rows = object.getJSONArray("rows");

                            JSONObject xx = rows.getJSONObject(0);


                            JSONArray imagePaths = xx.getJSONArray("imagePaths");

                            String yy = imagePaths.getString(0);


                            ftext1.setText("当前行驶公里数:" + 6 + "\n" + "\n" + "车辆距离下次保养剩余公里数:" + 7);


                     Glide.with(Fragment1.this).load("http://124.93.196.45:10001//dev-api/"+yy).into(imageView);






                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_1, container, false);
    }
}